package com.sicau.service.impl;

import com.sicau.dao.ManagerMapper;
import com.sicau.entity.dto.*;
import com.sicau.entity.pojo.po.ComposerPO;
import com.sicau.entity.pojo.vo.ResultVO;
import com.sicau.service.ManagerService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class ManagerServiceImpl implements ManagerService {

    @Autowired
    ManagerMapper managerMapper;

    @Override
    public ResultVO login(Manager manager) {
        Manager queryManager= managerMapper.queryLogin(manager);
        if(queryManager!=null){
            return new ResultVO(0,"success",queryManager);
        }else{
            return new ResultVO(-1,"failure");
        }
    }

    @Override
    public ResultVO index(){
        try {
            List<TaskInfo> list = managerMapper.getAllTask();
            return new ResultVO(0,"success",list);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultVO(-1,"获取任务列表查询失败");
        }
    }

    @Override
    public ResultVO composerInfo(String id){
        try {
            JSONObject data = new JSONObject();
            //查询更新时间
            data.put("updateTime", managerMapper.selectUpdateTime(id));
            //查询公告信息
            List<Notice> list = managerMapper.getAllNotice();
            JSONArray jsonArray =JSONArray.fromObject(list);
            data.put("notice", jsonArray);
            //查询个人信息
            JSONObject jsonObject = JSONObject.fromObject(managerMapper.selectPersonalInfoById(id));
            jsonObject.put("userName", managerMapper.selcetComposerById(id).getUserName());
            data.put("personalInfo", jsonObject);
            return new ResultVO(0,"success",data);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultVO(-1,"根据id查询失败");
        }

    }

    @Override
    public ResultVO setTask(String option,TaskInfo taskInfo){
        //先判断是否有该用户
        if(managerMapper.selectPersonalInfoById(taskInfo.getComposerId())==null){
            return new ResultVO(1,"查无此用户id");
        }

        UpdateTime updateTime = new UpdateTime();
        updateTime.setComposerId(taskInfo.getComposerId());
        updateTime.setUpdateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));


        if (option.trim().equals("add")) {//如果是新建任务则进入此
            try {
                taskInfo.setTaskId(UUID.randomUUID().toString().replaceAll("-",""));
                managerMapper.addTask(taskInfo);
                managerMapper.setUpdateTime(updateTime);//设置更新时间
                return new ResultVO(0,"success");
            } catch (Exception e) {
                e.printStackTrace();
                return new ResultVO(-1,"新建任务失败");
            }
        } else if(option.trim().equals("modify")){//如果是修改任务则进入此
            try {
                if (managerMapper.selectTaskById(taskInfo.getTaskId())==null){
                    return new ResultVO(2,"查无此任务id");
                }
                managerMapper.modifyTask(taskInfo);
                managerMapper.setUpdateTime(updateTime);//设置更新时间
                return new ResultVO(0,"success");
            } catch (Exception e) {
                e.printStackTrace();
                return new ResultVO(-1,"修改任务失败");
            }
        }
        return new ResultVO(-1,"非法操作");
    }

    @Override
    public ResultVO deleteTask(String taskId){
        try {
            UpdateTime updateTime = new UpdateTime();
            updateTime.setComposerId(taskId);
            updateTime.setUpdateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
            managerMapper.deleteTaskById(taskId);
            managerMapper.setUpdateTime(updateTime);
            return new ResultVO(0,"success");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultVO(-1,"删除任务失败");
        }
    }

    @Override
    public ResultVO modifyNotice(Notice notice){
        try {
            //获取系统时间
            Date day=new Date();
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            System.out.println(df.format(day));
            notice.setPublishTime(df.format(day));

            managerMapper.modifyNotice(notice);
            return new ResultVO(0,"success");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultVO(-1,"修改公告失败");
        }
    }

    @Override
    public ResultVO getComposer(){
        try {
            List<Composer> list = managerMapper.getAllComposer();
            return new ResultVO(0,"success",list);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultVO(-1,"获取账户信息失败");
        }
    }

    @Override
    public ResultVO composer(String id){

        try {
            JSONObject jsonObject = JSONObject.fromObject(managerMapper.selectPersonalInfoById(id));//先查用户信息
            Composer composer = managerMapper.selcetComposerById(id);
            jsonObject.put("password", composer.getPassword());//再查密码
            jsonObject.put("userName",composer.getUserName());
            jsonObject.put("referee", composer.getReferee());
            jsonObject.put("groupName", composer.getGroupName());
            return new ResultVO(0,"success",jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultVO(-1,"查询用户信息失败");
        }

    }

    @Override
    public ResultVO setAccount(String option, ComposerPO composerPO){
        try {

            if (option.equals("add")){
                if (managerMapper.selectManagerById(composerPO.getManagerId())==null){
                    return new ResultVO(2,"管理员id不正确");
                }
                String id = UUID.randomUUID().toString().replaceAll("-","");
                composerPO.setId(id);//此时id为用户的id
                String password = ""+(int)((Math.random()*9+1)*100000)+"";
                composerPO.setPassword(password);
                managerMapper.addComposer(composerPO);//添加数据到composer表
                managerMapper.addPersonalInfo(composerPO);//添加数据到personalInfo表

                UpdateTime updateTime = new UpdateTime();
                updateTime.setComposerId(composerPO.getId());
                updateTime.setUpdateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));

                managerMapper.addUpdateTime(updateTime);
                managerMapper.setUpdateTime(updateTime);

                JSONObject jsonObject = new JSONObject();
                jsonObject.put("id",id);
                jsonObject.put("password", password);
                jsonObject.put("realName", composerPO.getRealName());
                return new ResultVO(0,"success",jsonObject);
            }else if (option.equals("modify")){
                //先判断是否有该用户
                if(managerMapper.selectPersonalInfoById(composerPO.getId())==null){
                    return new ResultVO(1,"查无此用户id");
                }
                if (managerMapper.selectManagerById(composerPO.getManagerId())==null){
                    return new ResultVO(2,"管理员id不正确");
                }
                managerMapper.updateComposer(composerPO);
                managerMapper.updatePersonalInfo(composerPO);
                return new ResultVO(0,"success");
            }
            else{
                return new ResultVO(-1,"非法操作");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultVO(-1,"修改数据时服务器报错");
        }
    }

    @Override
    public ResultVO changeComposerPassword(Composer composer){
        try {
            managerMapper.changeComposerPassword(composer);
            return new ResultVO(0,"success");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultVO(-1,"修改密码失败");
        }
    }

    @Override
    public ResultVO changeComposerStatus(Composer composer){
        try {
            managerMapper.changeComposerStatus(composer);
            return new ResultVO(0,"success");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultVO(-1,"修改状态失败");
        }
    }



}
